﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Bus;

namespace Control.Dal
{
    class DalProduct
    {

        internal static Control.Bus.BusProduct GetProdByCode(string MaProd)
        {
            var Prods = from p in DalCommon.Database.Products
                        where p.ProdCode == MaProd
                        select new BusProduct
                        {
                            ID = p.ID,
                            ProductCode = p.ProdCode,
                            ProductName = p.ProdName,
                            Shelf = BusShelf.GetByID(p.Shelf.ID)
                        };
            return Prods.FirstOrDefault<BusProduct>();
        }



        

        internal static List<BusProduct> GetAll()
        {
            var prods = from p in DalCommon.Database.Products
                        select new BusProduct
                        {
                            ID = p.ID,
                            ProductCode = p.ProdCode,
                            ProductName = p.ProdName,
                            Shelf = BusShelf.GetByID(p.Shelf.ID)
                        };
            return prods.ToList<BusProduct>();
        }

        internal static int Insert(BusProduct busProduct)
        {
            Product p = new Product()
            {
                ProdCode = busProduct.ProductCode,
                ProdName = busProduct.ProductName,
                IDShelf = busProduct.Shelf.ID,
            };
            DalCommon.Database.Products.InsertOnSubmit(p);
            DalCommon.Database.SubmitChanges();
            return p.ID;
        }

        internal static void Update(BusProduct busProduct)
        {
            WareHouseDataContext db = DalCommon.Database;
            Product p = db.Products.Single(r => r.ID == busProduct.ID);
            p.ProdCode = busProduct.ProductCode;
            p.ProdName = busProduct.ProductName;
            p.IDShelf = busProduct.Shelf.ID;
            db.SubmitChanges();
        }

        internal static void Delete(BusProduct busProduct)
        {
            try
            {
                WareHouseDataContext db = DalCommon.Database;
                Product p = db.Products.Single(r => r.ID == busProduct.ID);
                db.Products.DeleteOnSubmit(p);
                db.SubmitChanges();
            }
            catch
            {

            }
        }

        internal static List<BusProduct> GetListByIDKe(int IDPKe)
        {
            var prods = from p in DalCommon.Database.Products
                        where p.IDShelf==IDPKe
                        select new BusProduct
                        {
                            ID = p.ID,
                            ProductCode = p.ProdCode,
                            ProductName = p.ProdName,
                            Shelf = BusShelf.GetByID(p.Shelf.ID)
                        };
            return prods.ToList<BusProduct>();
        }

        internal static List<BusProduct> Find(string _p)
        {
            var prods = from p in DalCommon.Database.Products
                        where p.ProdCode.Contains(_p)||p.ProdName.Contains(_p)
                        select new BusProduct
                        {
                            ID = p.ID,
                            ProductCode = p.ProdCode,
                            ProductName = p.ProdName,
                            Shelf = BusShelf.GetByID(p.Shelf.ID)
                        };
            return prods.ToList<BusProduct>();
        }
    }
}
